﻿/*******************************************************************************
 * Copyright © 2016
 * 
 * Description: MVC快速开发平台  FROM http://xmwxgn.com
 *
*********************************************************************************/
using ZFine.Code;
using ZFine.Domain.Entity.Gas;
using ZFine.Domain.IRepository.Gas;
using ZFine.Repository.Gas;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ZFine.Application.Gas
{
    public class FaceRecordApp
    {
        private IFaceRecordRepository service = new FaceRecordRepository();

        /// <summary>
        /// 客户端插入
        /// </summary>
        /// <param name="chargeRecordEntity"></param>
        public void ClientInsert(FaceRecordEntity faceRecordEntity)
        {
            faceRecordEntity.ClientCreate();
            service.Insert(faceRecordEntity);
        }
        /// <summary>
        /// 客户端修改
        /// </summary>
        /// <param name="chargeRecordEntity"></param>
        public void ClientUpdate(FaceRecordEntity faceRecordEntity)
        {
            faceRecordEntity.ClientUpdate();
            service.Update(faceRecordEntity);
        }
        
        public List<FaceRecordEntity> GetList(Pagination pagination,string RName, string IDNumber, string StartDate, String EndDate,string txtResult)
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.RName.Contains(RName));
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber.Contains(IDNumber));
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                DateTime start = DateTime.Parse(StartDate + " 00:00:00");
                expression = expression.And(t => t.RecordTime >= start);
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                DateTime end = DateTime.Parse(EndDate + " 23:59:59");
                expression = expression.And(t => t.RecordTime <= end);
            }
            if ("0" !=txtResult)
            {
                expression = expression.And(t => t.Result.Trim().Equals(txtResult));
            }
            return service.FindList(expression, pagination);
        }
        public List<FaceRecordEntity> GetMonthList(int Year, int Month)
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
            if (Year>0)
            {
                expression = expression.And(t => t.Year==Year);
            }
            if (Month > 0)
            {
                expression = expression.And(t => t.Month == Month);
            }
            return service.IQueryable(expression).ToList();
        }
        public List<FaceRecordEntity> GetPageMonthList(Pagination pagination, string RName, string IDNumber, string Year, string Month,string txtResult)
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.RName.Contains(RName));
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber.Contains(IDNumber));
            }
            if (!string.IsNullOrEmpty(Year))
            {
                int year = int.Parse(Year);
                expression = expression.And(t => t.Year == year);
            }
            if (!string.IsNullOrEmpty(Month))
            {
                int month = int.Parse(Month);
                expression = expression.And(t => t.Month == month);
            }
            if ("0" != txtResult)
            {
                expression = expression.And(t => t.Result.Trim().Equals(txtResult));
            }
            return service.FindList(expression, pagination);
        }
        public List<FaceRecordEntity> GetList( string IDNumber = "")
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            return service.IQueryable(expression).OrderBy(t => t.IDNumber).ToList();
        }
        public List<FaceRecordEntity> GetList(string IDNumber , int Year, int Month,int Day)
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            if (Year > 0)
            {
                expression = expression.And(t => t.Year == Year);
            }
            if (Month > 0)
            {
                expression = expression.And(t => t.Month == Month);
            }
            if (Year > 0)
            {
                expression = expression.And(t => t.Year == Year);
            }
            if (Day > 0)
            {
                expression = expression.And(t => t.Day == Day);
            }
            return service.IQueryable(expression).OrderBy(t => t.IDNumber).ToList();
        }
        public List<FaceRecordEntity> GetListByPlateNumber(string IDNumber)
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
           expression = expression.And(t => t.IDNumber == IDNumber);
            return service.IQueryable(expression).OrderBy(t => t.IDNumber).ToList();
        }
        public IQueryable<FaceRecordEntity> GetIQueryable(string IDNumber = "")
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber == IDNumber);
            }
            return service.IQueryable(expression);
        }
        /// <summary>
        /// 获得数量
        /// </summary>
        public int GetCount()
        {
            return service.IQueryable().Count();
        }
        /// <summary>
        ///  按年获得数量
        /// </summary>
        public int GetCount(int Year)
        {
            return service.IQueryable().Where(a =>  a.Year == Year)
                .Where(a => a.Result == ZFine.Application.Enum.Gas.FaceRecordResult.已刷卡.ToString()).Count();
        }
        /// <summary>
        ///  按年月获得数量
        /// </summary>
        public int GetCount(int Year, int Month)
        {
            return service.IQueryable().Where(a=>a.Year==Year&&a.Month==Month)
                .Where(a => a.Result == ZFine.Application.Enum.Gas.FaceRecordResult.已刷卡.ToString()).Count();
        }
        /// <summary>
        ///  按天获得数量
        /// </summary>
        public int GetCount(int Year, int Month, int day)
        {
            return service.IQueryable().Where(a => a.Year == Year && a.Month == Month&a.Day == day)
                .Where(a => a.Result == ZFine.Application.Enum.Gas.FaceRecordResult.已刷卡.ToString()).Count();
        }
        public FaceRecordEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }
        public void DeleteForm(string keyValue)
        {
            //if (service.IQueryable().Count(t => t.F_ParentId.Equals(keyValue)) > 0)
            //{
            //    throw new Exception("删除失败！操作的对象包含了下级数据。");
            //}
            //else
            //{
            //    service.Delete(t => t.F_Id == keyValue);
            //}
        }
        public void SubmitForm(FaceRecordEntity organizeEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                organizeEntity.Modify(keyValue);
                service.Update(organizeEntity);
            }
            else
            {
                organizeEntity.Create();
                service.Insert(organizeEntity);
            }
        }
        //删除
        public void Del(string keyValue)
        {
            service.Delete(a=>a.F_Id==keyValue);
        }
        /// <summary>
        ///  月份关联的刷卡数（默认前12个月）
        /// </summary>
        public String GetCountGroupByMonth()
        {
            DateTime dt = DateTime.Now;
            int[] arrFaceCount = new int[12];
            for (int i = -11; i <= 0; i++)
            {
                int curYear = dt.AddMonths(i).Year;
                int curMonth = dt.AddMonths(i).Month;
                arrFaceCount[i + 11] = GetCount(curYear, curMonth);
            }
            return string.Join(",", arrFaceCount);
        }

        /// <summary>
        ///  月份关联的满意度数（默认当前月）
        /// </summary>
        public string GetSatisfactionByMonth()
        {
            DateTime dt = DateTime.Now;
            var expression = ExtLinq.True<FaceRecordEntity>();
            expression = expression.And(a => a.Year == dt.Year && a.Month == dt.Month);
            List<FaceRecordEntity> list = service.IQueryable(expression)
                .Where(a => a.Result == ZFine.Application.Enum.Gas.FaceRecordResult.已刷卡.ToString()).ToList();
            var results = list.GroupBy(f => new { f.Satisfaction })
                            .Select(group => new { key=group.Key, count = group.Count() });
            List<string> resultList = new List<string>();
            foreach (var value in results)
            {
                var str = value.count + "," + value.key.Satisfaction;
                resultList.Add(str);
            }
            return string.Join("-", resultList.ToArray());
        }

        public List<FaceRecordEntity> GetExportList(string RName, string IDNumber, string StartDate, String EndDate, string txtResult)
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.RName.Contains(RName));
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber.Contains(IDNumber));
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                DateTime start = DateTime.Parse(StartDate + " 00:00:00");
                expression = expression.And(t => t.RecordTime >= start);
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                DateTime end = DateTime.Parse(EndDate + " 23:59:59");
                expression = expression.And(t => t.RecordTime <= end);
            }
            if ("0" != txtResult)
            {
                expression = expression.And(t => t.Result.Trim().Equals(txtResult));
            }
            return service.IQueryable(expression).ToList();
        }

        /// <summary>
        ///  本周的识别数（默认本周开始到当前日）
        /// </summary>
        public List<string> GetCountGroupByWeek()
        {
            List<string> resultList = new List<string>();
            string[] arrDay = new string[8];
            int[] arrFaceCountByDay = new int[8];
            // 当前日期为往前推一周
            for (int i = -7; i <= 0; i++)
            {
                DateTime dt = DateTime.Now.AddDays(i);
                arrDay[i + 7] = dt.Month + "月" + dt.Day + "日";
                arrFaceCountByDay[i + 7] = service.IQueryable()
                    .Where(a => a.Year == dt.Year && a.Month == dt.Month && a.Day == dt.Day)
                    .Where(a => a.Result == ZFine.Application.Enum.Gas.FaceRecordResult.已刷卡.ToString()).Count();
            }
            
            string strDay = string.Join(",", arrDay);
            string strDiningCount = string.Join(",", arrFaceCountByDay);
            resultList.Add(strDay);
            resultList.Add(strDiningCount);
            resultList.Add(arrFaceCountByDay.Sum().ToString());
            return resultList;
        }

        public List<FaceRecordEntity> GetFaceTotalList(string RName, string IDNumber, string StartDate, String EndDate, string txtResult)
        {
            var expression = ExtLinq.True<FaceRecordEntity>();
            if (!string.IsNullOrEmpty(RName))
            {
                expression = expression.And(t => t.RName.Contains(RName));
            }
            if (!string.IsNullOrEmpty(IDNumber))
            {
                expression = expression.And(t => t.IDNumber.Contains(IDNumber));
            }
            if (!string.IsNullOrEmpty(StartDate))
            {
                DateTime start = DateTime.Parse(StartDate + " 00:00:00");
                expression = expression.And(t => t.RecordTime >= start);
            }
            if (!string.IsNullOrEmpty(EndDate))
            {
                DateTime end = DateTime.Parse(EndDate + " 23:59:59");
                expression = expression.And(t => t.RecordTime <= end);
            }
            if ("0" != txtResult)
            {
                expression = expression.And(t => t.Result.Trim().Equals(txtResult));
            }
            return service.IQueryable(expression).ToList();
        }
    }
}
